package com.ty.xxljob;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class OrderXxlJob {
    /**
     * 1. 简单任务示例 （Bean模式）
     */

    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        log.info("XXL-JOB, Hello World.");
    }

    /**
     * 4、跨平台Http任务
     *  参数示例：
     *      "url: http://www.baidu.com\n" +
     *      "method: get\n" +
     *      "data: content\n";
     */
    //{"url":"http://api.yytianqi.com/observe?city=CH010100&key=ulei88fgi91lb2k0",
    // "method":"get",
    // "data":""}
    @XxlJob("httpJobHandler")
    public void httpJobHandler() throws Exception {

        String jsonStr = XxlJobHelper.getJobParam();
        HashMap map =  JSONUtil.toBean(jsonStr, HashMap.class);
        String url = map.get("url").toString();
        String method = map.get("method").toString();
        String data = map.get("data").toString();
        if(method.equals("get")){
            String body= HttpRequest.get(url).execute().body();
            System.out.println( body);

        } else if (method.equals("post")){
            String body= HttpRequest.post(url).body(data).execute().body();

        }



    }
}
